﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Data.Sql;

namespace CSM.Views.Management
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetData : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string datasql = System.Web.HttpContext.Current.Request.QueryString["database"].ToString();
            string whichone = System.Web.HttpContext.Current.Request.QueryString["which"].ToString();
            if (System.Web.HttpContext.Current.Request.QueryString["q"] != null)
            {
                string key = System.Web.HttpContext.Current.Request.Params["q"].ToString();
                string result = "";
                string sql = "select distinct top 10 " + whichone + " from " + datasql + " where " + whichone + " like '" + key + "%'";
                SqlConnection myconn = new SqlConnection();
                myconn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CSMConnectionString"].ConnectionString;
                SqlCommand mycomm = new SqlCommand();
                mycomm.Connection = myconn;
                mycomm.CommandText = sql;
                myconn.Open();
                SqlDataReader dr = mycomm.ExecuteReader();
                while (dr.Read())
                {
                    result += dr[whichone].ToString() + "\n";
                }
                myconn.Close();
                if (result == "")
                    result = "not exists";
                context.Response.ContentType = "text/plain";
                context.Response.Write(result);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
